# -*- coding: utf-8 -*-
# time: 2025/4/9 11:42
# file: ch01.py
# author: hanson
from langchain import hub
from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain.tools import Tool
from langchain_core.prompts import ChatPromptTemplate
from langchain_ollama import ChatOllama

# 1. 使用LangChain Hub的标准提示
prompt = hub.pull("hwchase17/openai-tools-agent")

# 2. 定义工具
def calculate_fruits(number: str) -> str:
    return str(number)

math_tool = Tool(
    name="FruitCalculator",
    func=calculate_fruits,
    description="输入苹果数量，返回水果总数（苹果+4个桃子）"
)

# 3. 创建代理
llm = ChatOllama(model="qwen2.5:7b", temperature=0)
tools = [math_tool]
agent = create_tool_calling_agent(llm, tools, prompt)

# 4. 执行
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
result = agent_executor.invoke({
    "input": "如果桌上有5个苹果和4个桃子，共有多少水果？"
})

print(result)
print(result["output"])  # 应该输出 9